package morenoapp.components.thinkgraphv2.components;

import com.jgoodies.binding.adapter.Bindings;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import morenoapp.MorenoApp;
import morenoapp.components.thinkgraphv2.components.CheckBoxListTable.DataItem;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.model.enums.MorenoSex;
import morenoapp.model.listeners.MorenoModelAdapter;
import morenoapp.model.view2.MorenoView2Data;
import morenoapp.model.view2.MorenoView2Face;
import morenoapp.util.I18N;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author dmitry.mamonov
 */
public class RespondentDisplayConfigPanel extends javax.swing.JPanel {
    MorenoView2Face config;
    /** Creates new form RespondentDisplayConfigJPanel */
    public RespondentDisplayConfigPanel(MorenoView2Face _config) {
        this.config = _config;

        initComponents();
        initLayout();
    }

    private javax.swing.JCheckBox jCheckBoxHideFemale = new JCheckBox(I18N.get("sociogram.filter.hide_female"));
    private javax.swing.JCheckBox jCheckBoxHideMale = new JCheckBox(I18N.get("sociogram.filter.hide_male"));
    private javax.swing.JCheckBox jCheckBoxDisplayCorrections = new JCheckBox(I18N.get("sociogram.filter.display_corrections"));
    private javax.swing.JCheckBox jCheckBoxDisplayNames = new JCheckBox(I18N.get("sociogram.filter.display_names"));
    private javax.swing.JCheckBox jCheckBoxDisplaySex = new JCheckBox(I18N.get("sociogram.filter.display_sex"));
    private javax.swing.JScrollPane jScrollPaneLeft = new JScrollPane();
    private javax.swing.JScrollPane jScrollPaneRight = new JScrollPane();
    public MorenoView2Face getConfig() {
        return config;
    }

    private void initComponents() {
        //preffered size
        Dimension prefferedSize = new Dimension(95, 150);

        // male selector
        final CheckBoxListTable selectMaleTable = new CheckBoxListTable(new CheckBoxListTable.Handler<MorenoRespondentFace>() {
            @Override
            public List<DataItem<MorenoRespondentFace>> provideData() {
                List<DataItem<MorenoRespondentFace>> result = new ArrayList();
                for (MorenoRespondentFace resp : MorenoApp.getModel().getRespondentsList()) {
                    if (resp.getSex() == MorenoSex.MALE) {
                        result.add(new DataItem<MorenoRespondentFace>(resp, (resp.getIndex() + 1) + "." + resp.getName(), !config.isHideRespondent(resp)));
                    }
                }
                return result;
            }

            @Override
            public void applyChanges(DataItem<MorenoRespondentFace> aItem) {
                config.setHideRespondent(aItem.getId(), !aItem.isSelected());
            }

        });
        jScrollPaneLeft.setViewportView(selectMaleTable);
        jScrollPaneLeft.setPreferredSize(new Dimension(prefferedSize));

        // female selector
        final CheckBoxListTable selectFemaleTable = new CheckBoxListTable(new CheckBoxListTable.Handler<MorenoRespondentFace>() {
            @Override
            public List<DataItem<MorenoRespondentFace>> provideData() {
                List<DataItem<MorenoRespondentFace>> result = new ArrayList();
                for (MorenoRespondentFace resp : MorenoApp.getModel().getRespondentsList()) {
                    if (resp.getSex() == MorenoSex.FEMALE) {
                        result.add(new DataItem<MorenoRespondentFace>(resp, (resp.getIndex() + 1) + "." + resp.getName(), !config.isHideRespondent(resp)));
                    }
                }
                return result;
            }

            @Override
            public void applyChanges(DataItem<MorenoRespondentFace> aItem) {
                config.setHideRespondent(aItem.getId(), !aItem.isSelected());
            }

        });
        jScrollPaneRight.setViewportView(selectFemaleTable);
        jScrollPaneRight.setPreferredSize(new Dimension(prefferedSize));

        //add listeners
        MorenoApp.getModel().addModelListener(new MorenoModelAdapter() {
            @Override
            public void onRespondentChanged(MorenoRespondentFace changed) {
                selectMaleTable.refreshData();
                selectFemaleTable.refreshData();
            }

            @Override
            public void onRespondentsListChanged() {
                selectMaleTable.refreshData();
                selectFemaleTable.refreshData();
            }

        });

        Bindings.bind(jCheckBoxHideFemale, new PropertyAdapter(config, MorenoView2Data.PROPERTY_HIDEFEMALE, true));
        Bindings.bind(jCheckBoxHideMale, new PropertyAdapter(config, MorenoView2Data.PROPERTY_HIDEMALE, true));
        Bindings.bind(jCheckBoxDisplayCorrections, new PropertyAdapter(config, MorenoView2Data.PROPERTY_DISPLAYCORRECTIONS, true));
        Bindings.bind(jCheckBoxDisplayNames, new PropertyAdapter(config, MorenoView2Data.PROPERTY_DISPLAYNAMES, true));
        Bindings.bind(jCheckBoxDisplaySex, new PropertyAdapter(config, MorenoView2Data.PROPERTY_DISPLAYSEX, true));
    }

    private void initLayout() {
        //setup content
        FormLayout layout = new FormLayout(
                //columns
                "p:grow(50), 3dlu, p:grow(50)",
                //rows
                "p, p, p, p, p, p" +
                "");

        PanelBuilder builder = new PanelBuilder(layout, this);
        CellConstraints cc = new CellConstraints();

        int row = 1; //first row
        builder.add(jCheckBoxDisplayNames, cc.rcw(row, 1, 3));

        row++; //next row
        builder.add(jCheckBoxDisplayCorrections, cc.rcw(row, 1, 3));

        row++; //next row
        builder.add(jCheckBoxDisplaySex, cc.rcw(row, 1, 3));

        row++; //next row
        builder.addSeparator(I18N.get("sociogram.respondets_filter"), cc.rcw(row, 1, 3));

        row++; //next row
        builder.add(jCheckBoxHideMale, cc.rc(row, 1));
        builder.add(jCheckBoxHideFemale, cc.rc(row, 3));

        row++; //next row
        builder.add(jScrollPaneLeft, cc.rc(row, 1));
        builder.add(jScrollPaneRight, cc.rc(row, 3));
    }

    public static void main(String[] args) {
        MorenoUtil.debugShowComponentInFrame(new RespondentDisplayConfigPanel(MorenoApp.getModel().getView()));
    }

}
